package com.applink.sell_proof;

import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.TextView;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.table.CountryDBHelper;
import com.applink.sell_proof.model.Country;
import com.applink.sell_proof.model.Redeem;

public class RedeemDetail extends BaseActionBarActivity implements Observer {

	private static final String TAG = "RedeemDetail";
	private Redeem	redeem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.redeem_detail);
		setTitle("Redeem Details");
		DataHelper.getInstance().addObserver(this);
		Redeem redeem = (Redeem) getIntent().getSerializableExtra(Constant.INTENT_REDEEM_DETAIL);
		((TextView) findViewById(R.id.program_name)).setText(redeem.getProgramName());
		DataHelper.getInstance().redeemDetail(redeem.getId(), redeem.getTierId());
		showProgressDialog(false);
	}

	@Override
	public void update(Observable observable, Object data) {
		if (data == null) return;
		DataResult result = (DataResult) data;
		if (result.getMethodName() == Methods.getRedeemDetail) {
			if (result.getErrorCode() == null) {
				hideProgressDialog();
				redeem = (Redeem) result.getData();
				if (redeem != null) {
					String startDate = DateFormat.getDateFormat(this).format(new Date(redeem.getStartdate()));
					String endDate = DateFormat.getDateFormat(this).format(new Date(redeem.getEnddate()));
					((TextView) findViewById(R.id.tier_qualify)).setText(String.format("%s - %s", startDate, endDate));
					((TextView) findViewById(R.id.tier_product)).setText(redeem.getProduct());
					((TextView) findViewById(R.id.first_name)).setText(redeem.getFirstName());
					((TextView) findViewById(R.id.last_name)).setText(redeem.getLastName());
					((TextView) findViewById(R.id.mobile_profile)).setText(redeem.getMobile());
					((TextView) findViewById(R.id.email_profile)).setText(redeem.getEmail());
					((TextView) findViewById(R.id.street_name_profile)).setText(redeem.getStreet());
					((TextView) findViewById(R.id.building_name_profile)).setText(redeem.getBuilding());
					((TextView) findViewById(R.id.unit_profile)).setText(redeem.getUnit());
					((TextView) findViewById(R.id.postal_code_profile)).setText(redeem.getPostalCode());
					((TextView) findViewById(R.id.country_profile)).setText(redeem.getCountryName());
				}
			} else {
				DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case Dialog.BUTTON_NEGATIVE:
							dialog.dismiss();
							break;
						default:
							break;
						}
					}
				};
				showAlertDialog(listener,"Redeem","Close",null,result.getErrorCode().toString(), true);
			}
		} else if (result.getMethodName() == Methods.getAllCountry) {
			hideProgressDialog();
			List<Country> countries = CountryDBHelper.getAllCountry();
			if (countries != null) {
				initCountry(countries, (TextView) findViewById(R.id.country_profile) ,(TextView) findViewById(R.id.postal_code_profile));
			} else {
				Log.e(TAG, "no country");
			}
		}

	}
	
	private void initCountry(List<Country> countries, TextView country,TextView postalCode) {
		if (countries != null) {
			if(redeem != null){
				for(int i = 0 ; i < countries.size() ; i++){
					if(redeem.getCountryId() == countries.get(i).getCode()) {
						postalCode.setText(countries.get(i).getPostalCode());
						country.setText(countries.get(i).getName());
						break;
					}
				}
			}
		} 
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		DataHelper.getInstance().deleteObserver(this);
	}
}
